<?php
namespace app\api\controller;
use EasyWeChat\Factory;
use think\Log;
use think\Session;
use think\Db;
/**
 * 账户中心
 */
class Account extends Api{
    
    protected function _initialize(){
        parent::_initialize();
        if($this->auth->isLogin() == false){
            $url =  $this->request->url();
            Session::set('referUrl',$url);
            $this->redirect('Login/index');
            exit();
        }
    }
    /**
     * 我的
     */
    public function index(){
        $user = Db::name('User')->where('id',$this->auth->id)->find();
        $account = Db::name('Account')->where('id',$user['account_id'])->find();
        $this->assign('user',$user);
        $this->assign('account',$account);
        return $this->view->fetch();
    }
    /**
     * 充值记录
     */
    public function recharge(){
        $id = $this->auth->id;
        $list = Db::name('OrderRecharge')->alias('r')->join('__GOODS__ g','g.id = r.goods_id')->where('r.user_id',$id)->where('r.status',2)->field('r.id,r.order_sn,r.transaction_id,r.order_price,g.goods_price,g.extra_amount,r.pay_time')->limit(5)->select();
        $this->assign('list',$list);
        return $this->view->fetch();
    }
    /**
     * 消费记录
     */
    public function expense(){
        $id = $this->auth->id;
        $list = Db::name('OrderExpense')->alias('e')->join('__DEVICE__ d','d.id = e.device_id')->join('__DEVICE_PLACE__ p','p.id = d.place_id')->where('e.user_id',$id)->where('e.status',2)->field('e.id,e.order_sn,e.expense_amount,d.device_name,p.place_title,e.update_time')->limit(5)->select();
        return $this->view->fetch();
    }
}